<?php
/**
 * Created by: Dmitriy Demianchuk (d.demianchuk@gmail.com)
 * Date: 10.01.12 21:06
 */

class ImageInput extends EditorComponent
{
	private $imageGroupId = 0;

	private function registerJsCss(){
		$cs = Yii::app()->getClientScript();
		$imageInputAssets = Yii::app()->assetManager->publish(
			Yii::getPathOfAlias('adminArea.components.ATableEditor.Elements.ImageInput.assets'), true, -1, YII_DEBUG
		);
		$cs->registerCssFile($imageInputAssets.'/css/imageInput.css');
		$cs->registerScriptFile($imageInputAssets.'/js/imageInput.js');

		$colorBox = Yii::app()->assetManager->publish(
			Yii::getPathOfAlias('ext.jquery.plugins.*'), true, -1, YII_DEBUG
		);
		$cs->registerScriptFile($colorBox . '/colorbox/jquery.colorbox-min.js');
		$cs->registerCssFile($colorBox . '/colorbox/colorbox.css');

		$cs->registerScriptFile('/assets/messages/ru/imageInput.js', CClientScript::POS_BEGIN);
	}

	public function show(){
		$this->registerJsCss();
		$name = $this->getName();
		$imagesArr = array();
		if($this->current!==null){
			$this->imageGroupId = $this->current->image_group_id;
			if($this->imageGroupId!=0){
				$imagesArr = $this->getImages();
			}
		}
		echo $this->render('imageInput',array(
			'name'=>$name,
			'folderName'=>$this->model->tableName(),
			'imageGroupId'=>$this->imageGroupId,
			'imagesArr'=>$imagesArr,
		),true);
	}

	/**
	 * @return array - конфиг изображений которые привязаны к редактируемуму элементу
	 */
	private function getImages($imagesArr=array()){
		foreach ($this->current->imageGroup->image as $v) {
			$imagePath = Yii::app()->basePath . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR .
				AImageUpload::getImagesFolder() . DIRECTORY_SEPARATOR . $v->group->folder->folder_name .
				DIRECTORY_SEPARATOR . $v->group_id . DIRECTORY_SEPARATOR . $v->name;
			$image = array();
			if (!file_exists($imagePath)) {
				$image['error'] = Yii::t('imageInput','File not found');
			} else {
				$imageSize = getimagesize($imagePath);
				$image = array(
					'result' => true,
					'fileSize' => filesize($imagePath),
					'imageWidth' => $imageSize[0],
					'imageHeight' => $imageSize[1],
				);
			}
			array_push($imagesArr, array_merge($image, array(
				'imageGroupId' => $v->group_id,
				'priority' => $v->priority,
				'imageId' => $v->id,
				'fileName' => $v->name,
				'alt' => $v->alt,
				'uploadFolderName' => $v->group->folder->folder_name,
				'imageFolder' => AImageUpload::getImagesFolder(),
				'itemImage'=>$v->item_image,
			)));
		}
		return $imagesArr;
	}

	public function save($model){
		$name = $this->getName();
		$model->$name = $_POST['_form'][$name];
		$imageGroup = new ImageGroup();
		$imageGroup = $imageGroup->findByPk($model->$name);
		if($imageGroup!==null){
			$imageGroup->is_tmp = 0;
			$imageGroup->save();
		}
		if(isset($_POST['_formImgAlt'])){
			foreach($_POST['_formImgAlt'] as $k=>$v){
				$image = new Image();
				$image = $image->findByPk($k);
				if($image!=null){
					$image->alt = $v;
					if(isset($_POST['_formItemImage'][$k]) && $_POST['_formItemImage'][$k]==$k){
						$image->item_image = 1;
					}
					$image->save();
				}
			}
		}
		if(isset($_POST['_formImgMain'])){
			$image = new Image();
			$mainImage = $image->findByPk($_POST['_formImgMain']);
			if($mainImage!=null){
				$image::model()->updateAll(array('priority'=>0), 'group_id=:group_id AND priority=1', array(':group_id'=>$model->$name));
				$mainImage->priority = 1;
				$mainImage->save();
			}
		}
		return $model;
	}

	public function isNull(){

	}
}
